/**
 * Created by lwy on 2017/5/21.
 */

var remember;
var twice = false;
var verifyCode;
//$(function(){
//    if (typeof localStorage === 'object') {
//        try {
//            alert('支持');
//        } catch (e) {
//            alert('不支持');
//        }
//    }
//})
//点击验证码登录
$('#codelogin').click(function(){
    $('#passloginbox').hide();
    $('#codeloginbox').show();
    $('#login-button2').show();
    $('#phoneImg').attr('src','../img/login2-user2.png');
    $('#phoneImg').css({'width':'12px','height':'18px'})
    $('.code-button').show();
    $('#login-button1').hide();
    $(this).addClass('login2-active');
    $(this).siblings().removeClass('login2-active');
    ifOldUser();
})
var userId = '';
var isdzp = getItem('isdzp');
//判断是否为老用户
$(function () {
    userId = getItem('userId');

    if(userId==null||userId==undefined||userId==""){

    }else{
        document.getElementById('phone').oninput=function () {
            if($('#codelogin').hasClass('login2-active')){
                ifOldUser();
            }
            if($(this).val().length==11){
                $('#password').focus();
            }
        }
    }
})

function ifOldUser() {
    if($('#phone').val().length==11){
        //检查新老用户
        $.ajax({
            url:'/user/checkIfNewUser.json',
            dataType:'json',
            data:{
                mobile:$('#phone').val(),
                companyCode:getItem('companyCode'),
            },
            type:'post',
            success:function (data) {
                //code=1未注册 code=0错误信息 code=-1已注册
                if(data.code==1){
                    userId = getItem('userId');
                    $.ajax({
                        url:'/user/userMSG.json',
                        dataType:'json',
                        data:{
                            id:userId,
                            companyCode:getItem('companyCode'),
                        },
                        type:'post',
                        success:function (data) {
                            if(data.code==1){
                                $('.userBox').html('<span>邀请人:</span><span>'+data.datas.realName+'</span>');
                                $('.userBox').show();
                            }
                        }
                    })
                }
                if(data.code==-1){
                    $('.userBox').hide();
                    userId = '';
                }
                if(data.code==0){
                    if(data.msg=='请输入手机号'){

                    }else{
                        mui.toast(data.msg);
                    }
                }
            }
        })

    }
}

//点击账号密码登录
$('#passlogin').click(function(){
    $('#codeloginbox').hide();
    $('#passloginbox').show();
    $('#login-button1').show();
    $('#phoneImg').attr('src','../img/login2-user.png');
    $('#phoneImg').css({'width':'14px','height':'17px'})
    $('.code-button').hide();
    $('#login-button2').hide();
    $(this).addClass('login2-active');
    $(this).siblings().removeClass('login2-active');
})

//回车登录
function keyLogin(){
    if (event.keyCode==13){
        if($('#login-button1').is(':hidden')){
            document.getElementById("login-button2").click(); //调用登录按钮的登录事件
        }else if($('#login-button2').is(':hidden')){
            document.getElementById("login-button1").click(); //调用登录按钮的登录事件
        }
    }
}

//账号密码登录
function loginButton1(){
    var userName = $('#phone').val();
    var passWord = $('#password').val();
    var  myArray=[];
    myArray[0]=userName;
    myArray[1]=passWord;
    var  result = ifEmpty(myArray);
    if(result==0){
        mui.toast('用户名和密码不能为空');
        return;
    }
    if(!ifCyber()){
        mui.toast('请检查网络');
    }
    $.ajax({
        url:'/user/userLoginKey.json',
        dataType:'json',
        data:{
            name:userName,
            companyCode:getItem('companyCode'),
        },
        success:function(data){
            if(data.code!=1){
                mui.toast(data.msg);
                return;
            }
            if(data.code==1){
                passWord= calcMD5(passWord + data.datas.userKey, 32);
                $.ajax({
                    url:'/user/userLogin.json',
                    dataType:'json',
                    type:'post',
                    data:{
                        userName:userName,
                        passWord:remember ? getItem('passWord') :passWord,
                        companyCode:getItem('companyCode'),
                    },
                    success:function(data){
                        if(data.code!=1){
                            mui.toast(data.msg);
                            return;
                        }
                        if(data.code==1){
                            setItem('token',data.datas.token);
                            setItem('phone',userName);
                            setItem('userId',data.datas.userId);
                            setItem('passWord',remember ? getItem('passWord') : passWord);
                            window.location.href="index.html?companyCode="+getItem('companyCode');
                        }
                    }
                })
            }
        },
    })
}



//浏览器有填充,记住密码
$(function () {
    var name = getItem('phone');
    var pass = getItem('passWord');
    if (pass != null) {
        remember = true;
        $('#phone').val(name);
        //$('#password').val(pass);
        $('#password').val('15678455545');
        $('.eye').hide();
    } else {
        remember = false;
    }

});
$('#phone').keydown(function () {
    remember = false;
});
$('#password').keydown(function () {
    remember = false;
});

//清除按钮隐藏显示
document.getElementById('phone').oninput=function(){
    //document.activeElement.scrollIntoViewIfNeeded();
    if($(this).val()==""||$(this).val()=='请输入手机号'){
        $('.clear').hide();
    }else{
        $('.clear').show();
    }
    if($(this).val().length==11){
        $('#password').focus();
    }
}
//清除按钮清除输入框内容
$('.clear').click(function(){
    $('#phone').val('');
})
//密码明文
$('.eye').click(function(){
    if($('#password').attr('type')=='text'){
        $('#password').attr('type','password');
        $('.eye').css({'background':'url("../img/eye-hide.png") no-repeat 0 0','background-size':'17px 8.5px','width':'17px','height':'8.5px','top':'26px'})
    }else{
        $('#password').attr('type','text');
        $('.eye').css({'background':'url("../img/eye-show.png") no-repeat 0 0','background-size':'18px 9px','width':'18px','height':'9px','top':'22px'})
    }
})


//微信登录
document.getElementById('wxLogin').addEventListener('tap',function () {
    if(ifApp()==0){
        //微信
        window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcae7693294154eb4&redirect_uri=http%3a%2f%2fddt.chinaddcat.com%2fm%2fphoneHtml%2fbaiye.html&response_type=code&scope=snsapi_userinfo&state=wx&connect_redirect=1#wechat_redirect"
    }else if(ifApp()==1){
        /*ios*/
        setupWebViewJavascriptBridge(function(bridge) {
            /* Initialize your app here */
            bridge.callHandler('wxLogin', function(responseData) {
                var data = JSON.parse(responseData)
                commonLogin(data,'wx');
            })
        })
    }else if(ifApp()==2){
        //安卓
        window.ddw.wxlogin();
    }else{
        //其他
        window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcae7693294154eb4&redirect_uri=http%3a%2f%2fddt.chinaddcat.com%2fm%2fphoneHtml%2fbaiye.html&response_type=code&scope=snsapi_userinfo&state=wx&connect_redirect=1#wechat_redirect"
    }
})
//qq登录
document.getElementById('qqLogin').addEventListener('tap',function () {
    if(ifApp()==0){
        window.location.href="https://graph.qq.com/oauth2.0/authorize?client_id=101442252&response_type=code&redirect_uri=http%3a%2f%2fddt.chinaddcat.com%2fm%2fphoneHtml%2fbaiye.html&state=qq&display=mobile&g_ut=2"
    }else if(ifApp()==2){
        //安卓
        window.ddw.qqlogin();
    }else if(ifApp()==1){
        //ios
        setupWebViewJavascriptBridge(function(bridge) {
            /* Initialize your app here */
            bridge.callHandler('qqLogin', function(responseData) {
                var data = JSON.parse(responseData)
                commonLogin(data,'qq');
            })
        })
    }else{
        //其他
        window.location.href="https://graph.qq.com/oauth2.0/authorize?client_id=101442252&response_type=code&redirect_uri=http%3a%2f%2fddt.chinaddcat.com%2fm%2fphoneHtml%2fbaiye.html&state=qq&display=mobile&g_ut=2"
    }
})

//android
function appLogin(a,state) {
    a = JSON.parse(a);
    commonLogin(a.datas,state);
}

function commonLogin(data,state) {
    if(data.isOAuth==1){
        //绑定过手机号，登录成功跳转到首页
        setItem('token',data.token);
        setItem('phone',data.name);
        setItem('userId',data.userId);
        window.location.href="index.html";
    }else if(data.isOAuth==0){
        var accessToken = data.accessToken,
            openId = data.openId,
            Astate = state;
        window.location.href="bundCount.html?accessToken="+accessToken+'&openId='+openId+'&state='+Astate;
    }
}

var x=0;
$(document).ready(function(){
    $(window).resize(function() {
        x++;
        if(x%2==0){
            if(getItem('companyCode')==2){
                $('.login_fast').hide();
            }else{
                $('.login_fast').show()
            }
        }else{
            $('.login_fast').hide()
        }
    });

    if(getItem('companyCode')==2){
        $('.login_fast').hide();
    }else{
        $('.login_fast').show();
    }

});